package com.wlc.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author 王立朝
 * @date 2023/11/3
 * @description: MybatisPlus 新建方法，时间自增
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

	@Override
	public void insertFill(MetaObject metaObject) {
		String dateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
		//创建时间默认当前时间
		/*setFieldValByName("start_time", dateTime,metaObject);
		setFieldValByName("end_time", dateTime,metaObject);
		setFieldValByName("createTime", dateTime,metaObject);*/
		setFieldValByName("createTime", dateTime,metaObject);
		//this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
	}

	@Override
	public void updateFill(MetaObject metaObject) {
		String dateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
		/*String dateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
		setFieldValByName("end_time", dateTime,metaObject);
		setFieldValByName("updateTime", dateTime,metaObject);*/
		setFieldValByName("updateTime", dateTime,metaObject);
		//this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
		//this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
	}
}
